package com.delivery.deliverysystem.util;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于生成用户的用户名
 *
 * @author Bernie_fang
 */
public class CommonUtil {

    /**
     * 卡号生成规则
     * 编号：年份+是否为测试+7位随机数
     * @param isTest
     * @return
     */
    public static StringBuilder getCardNumber(Integer isTest) {
        StringBuilder cardNumber = new StringBuilder(getYear());
        cardNumber.append(isTest);
        for (int i = 0; i < 7; i++) {
            int i1 = (int) (Math.random() * 10);
            cardNumber.append(i1);
        }
        return cardNumber;
    }

    /**
     * 获取username编号
     * 规则：
     * 编号：年份+七位的百万数字
     * 账号：D+编号
     *
     * @param start 开始编号
     * @param end   结束编号
     * @return
     */
    public static List<String> getUsersNumberingList(Integer start, Integer end) {
        List<String> numberingList = new ArrayList<>();
        String currentYear = getYear();
        DecimalFormat decimalFormat = new DecimalFormat("0000000");
        for (int i = start; i <= end; i++) {
            String formatUsername = decimalFormat.format(i);
            String numbering = currentYear + formatUsername;
            numberingList.add(numbering);
        }
        return numberingList;
    }

    /**
     * 获取当天日期的年
     */
    public static String getYear() {
        LocalDateTime currentTime = LocalDateTime.now();
        Integer currentYear = currentTime.getYear();
        return currentYear.toString();
    }

    /**
     * 随机生成一个6位数的密码，纯数字
     *
     * @return
     */
    public static String getRandomPassword() {
        char[] randomChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        String randomPassword = String.valueOf(randomChar[random.nextInt(10)]);
        for (int i = 0; i < 5; i++) {
            randomPassword += String.valueOf(randomChar[random.nextInt(10)]);
        }
        return randomPassword;
    }


    /**
     * 生成不重复id
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

/*    *//**
     * 生成一个唯一的四位数
     *//*
    public static class UniqueNumberGenerator {
        private static final int MIN_VALUE = 1000;
        private static final int MAX_VALUE = 9999;
        private static AtomicInteger counter = new AtomicInteger(MIN_VALUE);

        public static String generateUniqueNumber() {
            int uniqueId = counter.getAndIncrement();
            if (uniqueId > MAX_VALUE) {
                counter.set(MIN_VALUE);
                uniqueId = counter.getAndIncrement();
            }
            return String.format("%04d", uniqueId);
        }
    }*/

    public static List<String> UniqueNumberGenerator(Integer start, Integer end) {
        List<String> numberingList = new ArrayList<>();
//        String currentYear = getYear();
        //String currentYear = getYear();
        DecimalFormat decimalFormat = new DecimalFormat("000000000");
        for (int i = start; i <= end; i++) {
            String formatUsername = decimalFormat.format(i);
            String numbering =formatUsername;
            numberingList.add(numbering);
        }
        return numberingList;
    }


    /**
     * 手机号正则表达式
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
        String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    /**
     * 邮箱正则表达式
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String check = "^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean isMatched = matcher.matches();
        return isMatched;
    }

    public static boolean isIdentityCard(String number){
        String rex = "[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]";
        return number.matches(rex);
    }

    /**
     * @Description: 用于截取每个学校的名字，比如：南京农业大学(一流学科建设高校)，截取 南京农业大学
     * @Author: Bernie_fang
     * @Since: 2021/6/7 20:52
     * @param collegeName: 大学名字，带括号
     * @return: java.lang.String    大学名字,不带括号
     **/
    public static String theNameBeforeTheParentheses(String collegeName) {
        int indexEnglish = collegeName.indexOf('(');//英文括号
        if (indexEnglish != -1){
            collegeName = collegeName.substring(0,indexEnglish);
        }
        int indexChinese = collegeName.indexOf('（');//中文括号
        if (indexChinese != -1){
            collegeName = collegeName.substring(0,indexChinese);
        }
        return collegeName;
    }


}
